﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.FormAdd_Edit;
using Manager.App_Code;
using System.Linq;
using System.Data.Linq;
using Manager.Common;
using Manager.ViewModels;

namespace Manager.FormMain
{
    public partial class Frm_BarButton_KhachHang : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
        List<KhachHangViewModel> lstKhachHang = null;

        public Frm_BarButton_KhachHang()
        {
            InitializeComponent();
            lstKhachHang = new List<KhachHangViewModel>();
        }

        private void Frm_BarButton_KhachHang_Load(object sender, EventArgs e)
        {
            lstKhachHang.Clear();
            db.Refresh(RefreshMode.OverwriteCurrentValues, db.KhachHangs);
            var lstTemp = db.KhachHangs
                            .OrderByDescending(t => t.SoNgayO)
                            .ToList();
            if (lstTemp != null)
            {
                foreach (var kh in lstTemp)
                {
                    KhachHangViewModel vmKhachHang = new KhachHangViewModel();
                    vmKhachHang.MaKhachHang = kh.MaKH;
                    vmKhachHang.TenKhachHang = kh.TenKH;
                    vmKhachHang.DiaChi = kh.DiaChi;
                    vmKhachHang.DienThoai = kh.DienThoai;
                    vmKhachHang.SoFax = kh.SoFax;
                    vmKhachHang.SinhNhat = kh.SinhNhat;
                    vmKhachHang.strSinhNhat = kh.SinhNhat == null ? "" : kh.SinhNhat.Value.ToString("dd/MM/yyyy");
                    vmKhachHang.SoTaiKhoan = kh.SoTaiKhoan;
                    vmKhachHang.HanTaiKhoan = kh.HanTaiKHoan;
                    vmKhachHang.NhomKhach = db.NhomKhaches.Single(i => i.MaNhomKhach == kh.MaNhomKhach);
                    vmKhachHang.TenNhomKhach = vmKhachHang.NhomKhach.TenNhomKhach;
                    vmKhachHang.CMND = kh.CMND;
                    vmKhachHang.Passport = kh.Passport;
                    vmKhachHang.HanTaiKhoan = kh.HanTaiKHoan;
                    vmKhachHang.SoTaiKhoan = kh.SoTaiKhoan;
                    vmKhachHang.SoNgayO = kh.SoNgayO;

                    lstKhachHang.Add(vmKhachHang);
                }
            }
            gridControl1.DataSource = null;
            if (lstKhachHang.Count > 0)
                gridControl1.DataSource = lstKhachHang;
        }

        private void butThemMoi_Click(object sender, EventArgs e)
        {
            frm_Customer Customer = new frm_Customer();
            Customer.ShowDialog();
            Frm_BarButton_KhachHang_Load(null, null);
        }

        private void butHieuChinh_Click(object sender, EventArgs e)
        {
            try
            {
                int index = gridView1.FocusedRowHandle;
                var maKH = CommonFunction.ParseLong(gridView1.GetRowCellValue(index, MaKH).ToString());
                var khachHang = db.KhachHangs.SingleOrDefault(i => i.MaKH == maKH);
                frm_Customer frmCustomer = new frm_Customer(khachHang);
                frmCustomer.ShowDialog();
                Frm_BarButton_KhachHang_Load(null, null);
            }
            catch (Exception ex)
            {
                Log.WriteLog("Frm_BarButton_KhachHang:butHieuChinh_Click" + ex.Message + "===>" + ex.StackTrace);
            }
        }

        private void butXoa_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = MessageBox.Show("Bạn có muốn xóa không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    int index = gridView1.FocusedRowHandle;
                    var maKH = CommonFunction.ParseLong(gridView1.GetRowCellValue(index, MaKH).ToString());
                    var khachHang = db.KhachHangs.Single(i => i.MaKH == maKH);
                    if (khachHang != null)
                    {
                        db.KhachHangs.DeleteOnSubmit(khachHang);
                        db.SubmitChanges();
                        MessageBox.Show("Xóa dữ liệu thành công!");
                        this.Frm_BarButton_KhachHang_Load(null, null);
                    }
                    else
                        MessageBox.Show("Xóa dữ liệu thất bại!");
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("Frm_BarButton_KhachHang:butXoa_Click" + ex.Message + "===>" + ex.StackTrace);
            }
        }

        private void btnFind_Click(object sender, EventArgs e)
        {            
            try
            {
                if (dateDenNgay.Text != "" && dateTuNgay.Text != "")
                {
                    var lstKhachHang = db.KhachHangs.Where(i => i.NgayTao >= dateTuNgay.DateTime && i.NgayTao <= dateDenNgay.DateTime).ToList();
                    gridControl1.DataSource = lstKhachHang;
                }
                else
                {
                    Frm_BarButton_KhachHang_Load(null, null);
                }
            }
            catch (Exception ex) 
            {
                Log.WriteLog("Frm_BarButton_KhachHang:btnFind_Click" + ex.Message + "===>" + ex.StackTrace);
                MessageBox.Show("Vui lòng xem lại dữ liệu nhập!");
            }
        }
    }
}